<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .shadow {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.4);
            position: absolute;
            left: 100px;
            top: 100px;
        }
    </style>
</head>

<body>
    <div class="shadow">

    </div>
</body>
<script>
    var shadow = document.querySelector(".shadow");

    shadow.onmousedown = function (event) {
        var event = event || window.event;

        //自己算
        var posX = event.pageX - shadow.offsetLeft;
        var posY = event.pageY - shadow.offsetTop;
        // console.log(posX , posY);

        document.onmousemove = function (e) {
            var event = event || window.event;
            var x = event.pageX - posX;
            var y = event.pageY - posY;
            console.log(x, y)
            shadow.style.left = x + "px";
            shadow.style.top = y + "px";
        }

        document.onmouseup = function () {
            document.onmousemove = null;
        }
    }
</script>

</html>